﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Globalization;

namespace Tireguit.Nmon.Parser.Information
{
    public class AAAElement:NmonInfoElement
    {
     
        public AAAElement():this(null)
        {
        }
        protected IDataParameter _typelineparam = null;
        protected IDataParameter _infolineparam = null;
        public string DateFichier { get; protected set; }
        public string HeureFichier { get; protected set; }
        public DateTime DateTimeFile
        {
            get
            {
                if (string.IsNullOrEmpty(DateFichier))
                {
                    return DateTime.MinValue;
                }
                if (string.IsNullOrEmpty(HeureFichier))
                {
                    return DateTime.MinValue;
                }
                DateTime res;
                if (DateTime.TryParseExact(DateFichier+" "+HeureFichier,"dd/MM/yy HH:mm.ss",CultureInfo.InvariantCulture,DateTimeStyles.None,out res))
                {
                    return res;
                }
                if (DateTime.TryParseExact(DateFichier + " " + HeureFichier, "dd-MMM-yyyy HH:mm.ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out res))
                {
                    return res;
                }
                if (DateTime.TryParse(DateFichier + " " + HeureFichier,  CultureInfo.InvariantCulture, DateTimeStyles.None, out res))
                {
                    return res;
                }
                return DateTime.MinValue;
            }
        }
        public AAAElement(IDbConnection connection)
        {
            ListCommand = new List<System.Data.IDbCommand>();
            Balise = BaliseType.AAA;
            Connection = connection;
            Linestring = string.Empty;
            _insertreq = "INSERT INTO " + NmonTables.TablesDictionnary.Instance["AAA"] + " ([command],[value]) VALUES ( @TypeLine, @InfoLine)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _typelineparam = Command.CreateParameter();
            _typelineparam.ParameterName = "@TypeLine";
            //_typelineparam.DbType = DbType.VarChar;
            _typelineparam.DbType = DbType.String;
            _infolineparam = Command.CreateParameter();
            _infolineparam.ParameterName = "@InfoLine";
            _typelineparam.DbType = DbType.String;
           // _infolineparam.SqlDbType = SqlDbType.VarChar;
        }
        
        protected new ILog _log = LogManager.GetLogger(typeof(AAAElement));
        public string TypeLine { get; protected set; }
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");
                

                if (Command==null)
                {
                    Command=Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);

                }
                Command.Parameters.Clear();
                _typelineparam.Value = TypeLine;
                _infolineparam.Value = InfoLine;
                Command.Parameters.Add(_typelineparam);
                Command.Parameters.Add(_infolineparam);
                
                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected  override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

               // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (!elements[0].StartsWith("AAA"))
                {
                    _log.Error("Not AAA Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                TypeLine = elements[1];
                InfoLine = elements[2];
                if (string.Compare(TypeLine,"date")==0)
                {
                    DateFichier = elements[2];
                }
                if (string.Compare(TypeLine,"time")==0)
                {
                    HeureFichier = elements[2];
                }
                return true;
              //  return aaaelem;

            }
            catch (System.Exception ex)
            {
            	_log.Error("InternalParse",ex);
               return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
